package itbour.onetouchshow.bean.inching.noppt;

import com.google.gson.Gson;

import java.util.List;

import itbour.onetouchshow.utils.L_;

/**
 * Created by zjl_d on 2017/11/22.
 */

public class NoPPTRootShapeValueV10Bean {

    public int ct;
    public int type;
    public int id;
    public int minRow;
    public int maxRow;
    public int minCol;
    public int maxCol;
    public List<DesignOrPrintTableBeanV10Row> children;


    /**
     * 刷新表格数据源, 给所有 cell 中的文本设置 rowType
     */

    public void refreshCellTextRowType() {
        for (int i = 0; i < children.size(); i++) {
            DesignOrPrintTableBeanV10Row row = children.get(i);
            for (int j = 0; j < row.children.size(); j++) {
                DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column cell = row.children.get(j);
                cell.children.get(0).rowType = row.rowType;
            }
        }
    }


    /**
     * 删除指定行
     *
     * @param rawPosition
     */
    public void removeRawPosition(int rawPosition) {

        if (children.size() <= 1 || rawPosition >= children.size()) {
            L_.i("removeRowErr: currentRowSize: " + children.size() + " removeIndex: " + rawPosition);
            return;
        }
        // 当删除第 零行的时候, 可能为表头, 进行表头转换处理
        if (rawPosition == 0) {
            // 执行删除行操作, 至少要有两行才能删除
            DesignOrPrintTableBeanV10Row titleRow = children.get(0);
            DesignOrPrintTableBeanV10Row nextRow = children.get(1);
            nextRow.rowType = titleRow.rowType;

            for (int i = 0; i < nextRow.children.size(); i++) {
                DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column cell = nextRow.children.get(i);
                cell.children.get(0).rowType = nextRow.rowType;
            }
        }
        children.remove(rawPosition);
    }

    /**
     * 删除指定列
     *
     * @param columnsPosition
     */
    public void removeColmnsPosition(int columnsPosition) {
        int rawSize = children.size();
        int columnsSize = children.get(0).children.size();
        for (int rawCount = 0; rawCount < rawSize; rawCount++) {
            for (int columnsCount = 0; columnsCount < columnsSize; columnsCount++) {
                if (columnsCount == columnsPosition) {
                    children.get(rawCount).children.remove(columnsCount);
                }
            }
        }
    }

    /**
     * 指定位置增加行
     *
     * @param row
     */
    public void addRawPositon(Integer row) {
        // 异常保护
        if (row > children.size()) {
            L_.i("addRowError: currentSize:" + children.size() + "insertRow: " + row);
            return;
        }
        DesignOrPrintTableBeanV10Row zeroData = children.get(0);
        DesignOrPrintTableBeanV10Row designOrPrintTableBeanV54Row = copyZeroColmnsData(zeroData);

        // 存在表头, 且插入第零行, 交换表头标记
        if (row == 0 && zeroData.rowType == 1) {
            zeroData.rowType = 0;
            designOrPrintTableBeanV54Row.rowType = 1;
        }
        // 更新数据源 rowtype
        for (int i = 0; i < zeroData.children.size(); i++) {
            DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column zeroCell = zeroData.children.get(i);
            zeroCell.children.get(0).rowType = zeroData.rowType;

            DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column insertCell = designOrPrintTableBeanV54Row.children.get(i);
            insertCell.children.get(0).rowType = designOrPrintTableBeanV54Row.rowType;
        }

        children.add(row, designOrPrintTableBeanV54Row);

    }

    public void addColumnPosition(Integer column) {
        int rawSize = children.size();
        int columnsSize = children.get(0).children.size();
        // 异常保护, 超出表格最大值的保护在 UI 层处理, 此处只管添加
        if (column > columnsSize) {
            L_.i("currentColumnSize: " + columnsSize + " -- insertColumnIndex: " + column);
            return;
        }
        DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column designOrPrintTableBeanV54Column = children.get(0).children.get(0);
        for (int rawCount = 0; rawCount < rawSize; rawCount++) {

            Gson gson = new Gson();
            String orangleJson = gson.toJson(designOrPrintTableBeanV54Column);
            DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column copyColmnData =
                    gson.fromJson(orangleJson, DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column.class);
            // 清空默认值
            copyColmnData.id = 0;
            copyColmnData.children.get(0).value = "";
            copyColmnData.children.get(0).tip = "";

            // 加入数据源
            DesignOrPrintTableBeanV10Row designOrPrintTableBeanV54Row = children.get(rawCount);
            List<DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column> children = designOrPrintTableBeanV54Row.children;
            L_.i("columnsCount===" + column + "column===" + column);

            // 设置对应数据源 的 rowtype
            copyColmnData.children.get(0).rowType = designOrPrintTableBeanV54Row.rowType;

            children.add(column, copyColmnData);
        }
    }

    /**
     * @param zeroData
     * @return
     */
    public DesignOrPrintTableBeanV10Row copyZeroColmnsData(DesignOrPrintTableBeanV10Row zeroData) {
        Gson gson = new Gson();
        String zeroJson = gson.toJson(zeroData);
        DesignOrPrintTableBeanV10Row copy = gson.fromJson(zeroJson, DesignOrPrintTableBeanV10Row.class);
        copy.rowType = 0;
        copy.id = 0;
        List<DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column> children = copy.children;
        int size = children.size();

        for (int i = 0; i < size; i++) {
            DesignOrPrintTableBeanV10Row.DesignOrPrintTableBeanV54Column copyColumn = children.get(i);
            copyColumn.id = 0;
            NoPPTRootTextValueV10Bean copyValue = copyColumn.children.get(0);
            copyValue.value = "";
            copyValue.tip = "";

        }
        return copy;
    }

    public static class DesignOrPrintTableBeanV10Row {
        public int ct;
        public int type;
        public int id;
        //rowType 如果为1 那么代表表头  背景颜色变黑
        public int rowType;
        public List<DesignOrPrintTableBeanV54Column> children;

        /**
         * ct : 815
         * type : 52
         * id : 8
         * children : [{"ct":808,"type":20,"id":9,"value":"默认文字","min":0,"max":4}]
         */
        //横着的
        public static class DesignOrPrintTableBeanV54Column {
            public int ct;
            public int type;
            public int id;
            public List<NoPPTRootTextValueV10Bean> children;

        }
    }
}
